<?php 
// Header
$this->set('title_for_layout', __("Chỉnh sửa sản phẩm", true));

$this->left_menu = 'menu/'.$this->params['controller'];
$params = array(
	'root'	=>	$this->params['named']['root']
);

if (isset($this->params['plugin'])) {
	$params['plugin'] = $this->params['plugin'];
} else {
	$params['plugin'] = null;
}
// -- Load CSS : Begin --
echo $this->Html->css('jstree/tree_component');
// -- Load CSS : End  --

// Load Javascript
echo $this->Html->script('ckeditor/ckeditor', array('type' => 'text/javascript'));
//echo $this->Html->script('jquery.form');
echo $this->Html->script('/products/js/edit');

if (count($categories)):
echo $this->Html->script('jstree/jquery.tree');
echo $this->Html->script('jstree/plugins/jquery.tree.checkbox');
echo $this->Html->script('jstree/libs/_lib');
endif;

?>
<?php 
// Xử lý các checkbox của category
$category_ids = array();
foreach ($this->data['Category'] as $item) {
	$category_ids[] = $item['id'];
}
?>
<script language="javascript">
// Tạo mảng các checkbox cho categories
var category_ids = [<?php echo implode(",", $category_ids)?>];

$(document).ready(function () {
	<?php if (count($categories)):?>
	the_tree = $("#category_tree").tree({
		ui : {
			theme_name : "checkbox",
			theme_path : "<?php echo $html->url('/css/jstree/themes/checkbox/style.css')?>"
		},
		plugins : { 
			checkbox : { }
		}
	});
	<?php endif;?>
	var select_categories_div = $('div#select_categories');
	select_categories_div.dialog({
		resizable: false,
		width: 400,
		autoOpen: false,
		modal: true
	});

	$('a#select_categories_link').click(function() {
		select_categories_div.dialog('open');
	});

	var the_form = $('form#edit_page_form');
	the_form.find('div.submit input[type="submit"]').click(function() {
		$('div#category_tree a.checked').each(function() {
			var check_id = $(this).parent().attr('node_id');
			the_form.append($("<input type='hidden' name='data[Category][]' value='"+check_id+"'/>"));
		});
		//return false;
	});

	$(category_ids).each(function() {
		
	});

	// Nếu số lượng category là lớn hơn 0 thì mới xử lý trường hợp này
	<?php if (count($categories)):?>
	for (var category_id in category_ids) {
		var node_id = "#phtml_"+category_ids[category_id]; 
		if ($.tree.reference(node_id)) {
			$.tree.plugins.checkbox.check(node_id)
		}
	}
	<?php endif;?>

	$('div#select_categories div.button_panel input[type="button"]').click(function() {
		select_categories_div.dialog('close');
	});

	$('div.buttons a.delete').click(function() {
		if (!confirm("<?php __("Bạn có thật sự muốn xóa sản phẩm này?")?>")) {
			return false;
		}
	});
});

</script>

<h2><?php __("Cập nhật sản phẩm")?></h2>
<?php echo $session->flash()?>

<div class="buttons attop">
	<ul>
		<li><a href="<?php echo $html->url($params + array('plugin' => 'products', 'controller' => 'manage_products', 'action'	=>	'index'))?>" class="back"><?php __("Danh sách sản phẩm")?></a></li>
		<li><a href="<?php echo $html->url($params + array('plugin' => 'products', 'controller' => 'manage_products', 'action'=>'delete', $this->data['Product']['id']))?>" class="cancel delete"><?php __("Xóa")?></a></li>
		<li><a href="javascript:void(0)" class="save"><?php __("Cập nhật")?></a></li>
	</ul>
</div>

<?php 
echo $form->create('Product', array(
	'url'	=>	
	$params + array(
		'controller'	=>	$this->params['controller'],	
		'action'		=>	'edit'
	),
	'id'		=>	'edit_page_form',
	'enctype'	=>	'multipart/form-data'
));
?>
<fieldset class="form">
<?php 
echo $form->input('id');
echo $form->input('title', array(
	'label'	=>	__("Tiêu đề: <font color=\"red\">(*)</font>", true),
	'error'	=>	__("Tiêu đề trang không được để trống", true),
	'size'	=> 50
));

if ($configs['Manage']['edit']['show_alias']) {
	echo $form->input('alias', array('label'	=>	__("Alias: ", true)));
} else {
	echo $form->input('alias', array('label'	=>	__("Alias: ", true), 'type' => 'hidden'));
}

if ($configs['Product']['image']['show']):
$image = $this->data['Product']['image'];
if ($image != "") {
	$ext = strrchr($image, ".");
	$pos = strrpos($image, ".");
	$filename = substr($image,0,$pos);
	$image = $filename."_thumbnail".$ext;
}

$options = array(
	'name'				=>	$image,
	'path'				=>	$configs['Product']['image']['uri'],
	'field_name'		=>	'image',
	'label'				=>	__('&nbsp;',true),
	'legend_title'		=>	__('Hình ảnh',true),
	'delete_image_link'	=>	$html->url($params + array('controller'=>$this->params['controller'], 'action'=>'delete_image'))."/page_id:".$this->data['Product']['id']
);
echo $html->form_image($options,array());
endif;
?>

<?php if ($configs['Product']['description']['show']):?>
<div class="input">
<table cellpadding='0' cellspacing='0' border='0' width='100%' class='list-table'>
<tr>
	<td class='label' valign='top'><label for="description"><?php __("Mô tả: ")?></label></td>
	<td>
		<?php
		echo $form->input('description', array('label'	=>	false, 'div'=>false, 'type'=>'textarea'));
		
		if (!isset($configs['Product']['description']['use_rich_text']) || $configs['Product']['description']['use_rich_text']):
		echo $wysiwyg->render("Product.description", 'ckeditor', $configs['Product']['description_config']);
		endif;
		?>
	</td>
</tr>
</table>
</div>
<?php endif;?>

<?php if ($configs['Product']['content']['show']):?>
<div class="input">
<table cellpadding='0' cellspacing='0' border='0' width='100%' class='list-table'>
<tr>
	<td class='label' valign='top'><label for="description"><?php __("Nội dung: ")?></label></td>
	<td>
		<?php
		echo $form->input('content', array('label'	=>	false, 'div'=>false, 'type'=>'textarea'));
		
		if (!isset($configs['Product']['description']['use_rich_text']) || $configs['Product']['description']['use_rich_text']):
		echo $wysiwyg->render("Product.content", 'ckeditor', $configs['Product']['content_config']);
		endif;
		?>
	</td>
</tr>
</table>
</div>
<?php endif;?>

<?php
echo $form->input('price', array(
	'label'	=>	__("Giá: ", true),
	'size'	=> 50,
    'default'   => '1.000.000 VND'
));
?>		
<?php if (count($categories)):?>
<div class='input text'>
	<label>&nbsp;</label>
	[ <a href='javascript:void(0)' id='select_categories_link'><?php __("Bấm vào đây để chọn danh mục")?></a> ]
</div>
<?php else:?>
<?php echo $form->input('Category', array('type'=>'hidden', 'value'=>$category_root_id, 'name'=>"data[Category][]"))?>
<?php endif;?>

<div id='dialogs' style="display:none">
<!-- Dialogs section : Begin -->
	<div id='select_categories' title="<?php __("Chọn danh mục")?>" class='form'>
		<?php 
		$settings = array(
			'data'			=>	$categories,
			'expand_all'	=>	true,
			'attributes'	=>	array(
				'id'	=>	'category_tree',
				'class'	=>	'demo'
			),
			'fields'		=>	array(
				'text'	=>	'name',
				'id'	=>	'id'
			),
			'model'	=>	'ProductCategory'
		);
		echo $this->element('tree/jstree_checkbox', array('settings'	=>	$settings));
		?>
		<div align='center' class='button_panel submit'>
			<input type='button' class='button' value='<?php __(" Đóng ")?>'/> 
		</div>
	</div>
<!-- Dialogs section : End -->
</div>

<?php
echo $form->input('is_published', array('label' => __("Kích hoạt?", true)));
if ($configs['Product']['special']['show']):
    echo $form->input('special', array('label' => __("Sản phẩm đặc biệt?", true)));
endif;
?>
<div style="display:none">
	<input type="submit"/>
</div>
</fieldset>
<?php echo $form->end();?>

<div class="buttons attop">
	<ul>
		<li><a href="<?php echo $html->url($params + array('plugin' => 'Product', 'controller' => 'manage_products', 'action'	=>	'index'))?>" class="back"><?php __("Danh sách sản phẩm")?></a></li>
		<li><a href="<?php echo $html->url($params + array('plugin' => 'Product', 'controller' => 'manage_products', 'action'=>'delete', $this->data['Product']['id']))?>" class="cancel delete"><?php __("Xóa")?></a></li>
		<li><a href="javascript:void(0)" class="save"><?php __("Cập nhật")?></a></li>
	</ul>
</div>